# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=qt6
pkgbase=mingw-w64-${_realname}-static
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}-static")
_qtver=6.2.1
pkgver=${_qtver/-/}
pkgrel=2
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://www.qt.io'
license=(GPL3 LGPL3 FDL custom)
pkgdesc="A cross-platform application and UI framework (mingw-w64)"
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader"
         "${MINGW_PACKAGE_PREFIX}-qtbinpatcher"
         "${MINGW_PACKAGE_PREFIX}-dbus"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         #"${MINGW_PACKAGE_PREFIX}-graphite2"
         #"${MINGW_PACKAGE_PREFIX}-harfbuzz"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libmng"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         #"${MINGW_PACKAGE_PREFIX}-md4c"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-pcre2"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
optdepends=("${MINGW_PACKAGE_PREFIX}-clang")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-clang-tools-extra"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-xmlstarlet"
             "${MINGW_PACKAGE_PREFIX}-vulkan-headers"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "rsync")
options=('!strip')
_pkgfn="${_realname/6/}-everywhere-src-${_qtver}"
_install_prefix=${MINGW_PREFIX}/qt6-static
source=("https://download.qt.io/official_releases/qt/${pkgver%.*}/${_qtver}/single/${_pkgfn}.tar.xz"
        001-adjust-qmake-conf-mingw.patch
        002-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch
        003-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch
        004-Allow-overriding-CMAKE_FIND_LIBRARY_SUFFIXES-to-pref.patch
        005-qt-6.2.0-win32static-cmake-link-ws2_32-and--static.patch
        006-Fix-finding-D-Bus.patch
        007-Fix-using-static-PCRE2-and-DBus-1.patch
        008-Fix-libjpeg-workaround-for-conflict-with-rpcndr.h.patch
        009-Fix-transitive-dependencies-of-static-libraries.patch
        010-Support-finding-static-MariaDB-client-library.patch
        011-Fix-crashes-in-rasterization-code-using-setjmp.patch
        012-Handle-win64-in-dumpcpp-and-MetaObjectGenerator-read.patch
        013-qmng-fix-build.patch
        014-fix-relocatable-prefix-staticbuild-v2.patch
        015-qt6-windeployqt-fixes.patch)
sha256sums=('e03fffc5c3b5fea09dcc161444df7dfbbe24e8a8ce9377014ec21b66f48d43cd'
            '26f3bcc3729520a5ba648a9ce92a9293c8fb1cf16d81af612587ce10d501dcb6'
            '2cce9659a2ae04e28f25e425852a0be28b3ba8080075c697d6c0e9b7bbfb7e5c'
            'af3f4c6491942ee6a7681eecef2d999897e38fcef7a5edcf3774589f137dac73'
            'fa73433aa123487562f365dd98735d44fb1effd38d13de4c0e33bd5f8e812396'
            '30c7925f5544ff15b170ea9cc126a674f10d0684a1d98692366d0dd3d94ee1d8'
            '5b3d7371b26ebcfc2a3d46a36db5dab4f50bb41b234d237021c32d21842a2880'
            '92eae9347dac68c1dfdd79d97fa9ac257ebffcb3364cb7693342dce5a182823b'
            '4b33eb1cd4cf43acd794bc5400a7224c96d6d0936f5589616beea3c470dc2165'
            '0610eac9a33738b65979a4cbc38451bd1ce824b7849e78f49997b7060f9ad7ad'
            '5ae7962c4959b5a6ab2147cbf094be60357d75daec8b4f8fa65aaf3c9e7cd3a6'
            '3f00105f55aca980e5bf3307a2bed76fa5adcc84748af8ad22b78e474e7fe3f3'
            '39d57493b0edc1317729b88dad6a07b86936072f0fbbd2aae7b01e2211ce29ed'
            'e445d8db19b9cce07f25bf0d324f1e894708d31ac4b6ab367f76a3fa190cc8c0'
            'c32e58e838c3c4f1a1dcf793415f26c2abefee83e99cccb4f86e7037844b6f2a'
            'abf3a39a0cf3173125b086f53f5d202e436194fdb897d928c813551db41df243')

# Use the right mkspecs file
if [[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]]; then
  _platform=win32-clang-g++
else
  _platform=win32-g++
fi

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd ${srcdir}

  sleep 2
  mv ${_pkgfn} qt${pkgver}
  sleep 2

  cd qt${pkgver}

  apply_patch_with_msg \
    001-adjust-qmake-conf-mingw.patch \
    002-qt-6.2.0-win32-g-Add-QMAKE_EXTENSION_IMPORTLIB-defaulting-to-.patch \
    003-qt-6.2.0-dont-add-resource-files-to-qmake-libs.patch \
    004-Allow-overriding-CMAKE_FIND_LIBRARY_SUFFIXES-to-pref.patch \
    005-qt-6.2.0-win32static-cmake-link-ws2_32-and--static.patch \
    006-Fix-finding-D-Bus.patch \
    007-Fix-using-static-PCRE2-and-DBus-1.patch \
    008-Fix-libjpeg-workaround-for-conflict-with-rpcndr.h.patch \
    009-Fix-transitive-dependencies-of-static-libraries.patch \
    010-Support-finding-static-MariaDB-client-library.patch \
    011-Fix-crashes-in-rasterization-code-using-setjmp.patch \
    012-Handle-win64-in-dumpcpp-and-MetaObjectGenerator-read.patch \
    013-qmng-fix-build.patch \
    014-fix-relocatable-prefix-staticbuild-v2.patch \
    015-qt6-windeployqt-fixes.patch

  local _ARCH_TUNE=
  local _HARD_FLAGS=
  case ${MINGW_CHOST} in
    i686*)
      _ARCH_TUNE="-march=i686 -mtune=core2"
      if [ "${_enable_hardening}" = "yes" ]; then
        _HARD_FLAGS="-Wl,--dynamicbase,--nxcompat,--no-seh"
      fi
    ;;
    x86_64*)
      _ARCH_TUNE="-march=nocona -mtune=core2"
      if [ "${_enable_hardening}" = "yes" ]; then
        _HARD_FLAGS="-Wl,--dynamicbase,--high-entropy-va,--nxcompat,--default-image-base-high"
      fi
    ;;
  esac

  BIGOBJ_FLAGS="-Wa,-mbig-obj"

  # Append these ones ..
  sed -i "s|^QMAKE_CFLAGS .*= \(.*\)$|QMAKE_CFLAGS            = \1 ${_ARCH_TUNE} ${BIGOBJ_FLAGS} ${LTCG_CFLAGS}|g" qtbase/mkspecs/${_platform}/qmake.conf
  sed -i "s|^QMAKE_LFLAGS           +=\(.*\)$|QMAKE_LFLAGS            += \1 ${_HARD_FLAGS}|g" qtbase/mkspecs/common/gcc-base.conf
}

build() {
  cd ${srcdir}
  [[ -d B${MSYSTEM} ]] && rm -rf B${MSYSTEM}
  mkdir -p B${MSYSTEM} && cd B${MSYSTEM}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DCMAKE_CONFIGURATION_TYPES=;-DCMAKE_FIND_LIBRARY_SUFFIXES=" \
  cmake \
    -G "Ninja Multi-Config" \
    -DCMAKE_CONFIGURATION_TYPES="Release;Debug" \
    -DCMAKE_FIND_LIBRARY_SUFFIXES_OVERRIDE=".a;.dll.a" \
    -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS} -static -static-libgcc -static-libstdc++" \
    -DBUILD_SHARED_LIBS=OFF \
    -DQT_QMAKE_TARGET_MKSPEC=${_platform} \
    -DCMAKE_INSTALL_PREFIX=${_install_prefix} \
    -DINSTALL_BINDIR=bin \
    -DINSTALL_LIBDIR=lib \
    -DINSTALL_INCLUDEDIR=include/qt6 \
    -DINSTALL_ARCHDATADIR=share/qt6 \
    -DINSTALL_DOCDIR=share/doc/qt6 \
    -DINSTALL_DATADIR=share/qt6 \
    -DINSTALL_MKSPECSDIR=share/qt6/mkspecs \
    -DINSTALL_DESCRIPTIONSDIR=share/qt6/modules \
    -DINSTALL_TESTSDIR=share/qt6/tests \
    -DINSTALL_EXAMPLESDIR=share/doc/qt6/examples \
    -DFEATURE_static_runtime=ON \
    -DFEATURE_relocatable=ON \
    -DFEATURE_openssl_linked=ON \
    -DINPUT_openssl=linked \
    -DINPUT_dbus=linked \
    -DINPUT_mng=yes \
    -DINPUT_libmd4c=qt \
    -DFEATURE_glib=OFF \
    -DINPUT_qt3d_assimp=qt \
    -DINPUT_quick3d_assimp=qt \
    -DFEATURE_system_assimp=OFF \
    -DFEATURE_system_doubleconversion=OFF \
    -DFEATURE_system_freetype=ON \
    -DFEATURE_system_harfbuzz=ON \
    -DFEATURE_hunspell=OFF \
    -DFEATURE_system_hunspell=OFF \
    -DFEATURE_system_jpeg=ON \
    -DFEATURE_system_pcre2=ON \
    -DFEATURE_system_png=ON \
    -DFEATURE_system_sqlite=OFF \
    -DFEATURE_system_tiff=ON \
    -DFEATURE_system_webp=ON \
    -DFEATURE_system_zlib=ON \
    -DFEATURE_opengl=ON \
    -DFEATURE_opengl_desktop=ON \
    -DFEATURE_egl=OFF \
    -DFEATURE_gstreamer=OFF \
    -DFEATURE_icu=OFF \
    -DFEATURE_fontconfig=OFF \
    -DFEATURE_pkg_config=ON \
    -DFEATURE_vulkan=ON \
    -DFEATURE_sql_ibase=OFF \
    -DFEATURE_sql_psql=OFF \
    -DFEATURE_sql_mysql=OFF \
    -DFEATURE_sql_odbc=OFF \
    -DFEATURE_zstd=OFF \
    -DFEATURE_wmf=ON \
    -DQT_BUILD_TESTS=OFF \
    -DQT_BUILD_EXAMPLES=OFF \
    -DBUILD_qttools=ON \
    -DBUILD_qtdoc=OFF \
    -DBUILD_qttranslations=ON \
    -DBUILD_qtwebengine=OFF \
    -DOPENSSL_DEPENDENCIES="-lws2_32;-lgdi32;-lcrypt32" \
    -DPOSTGRESQL_DEPENDENCIES="-lpgcommon;-lpgport;-lintl;-lssl;-lcrypto;-lshell32;-lws2_32;-lsecur32;-liconv" \
    -DMYSQL_DEPENDENCIES="-lssl;-lcrypto;-lshlwapi;-lgdi32;-lws2_32;-lpthread;-lz;-lm" \
    -DLIBPNG_DEPENDENCIES="-lz" \
    -DGLIB2_DEPENDENCIES="-lintl;-lws2_32;-lole32;-lwinmm;-lshlwapi;-lm" \
    -DFREETYPE_DEPENDENCIES="-lbz2;-lharfbuzz;-lfreetype;-lbrotlidec;-lbrotlicommon" \
    -DHARFBUZZ_DEPENDENCIES="-lglib-2.0;-lintl;-lws2_32;-lusp10;-lgdi32;-lole32;-lwinmm;-lshlwapi;-lintl;-lm;-lfreetype;-lgraphite2;-lrpcrt4" \
    -DLIBBROTLIDEC_DEPENDENCIES="-lbrotlicommon" \
    -DLIBBROTLIENC_DEPENDENCIES="-lbrotlicommon" \
    -DLIBBROTLICOMMON_DEPENDENCIES="" \
    -DDBUS1_DEPENDENCIES="-lws2_32;-liphlpapi;-ldbghelp" \
    ../qt${pkgver}

  export PATH=$PWD/bin:$PATH

  cmake --build .
}

package() {
  DESTDIR=${pkgdir} ninja -C B${MSYSTEM} install

  install -d "${pkgdir}"${_install_prefix}/share/licenses/${_realname}
  install -Dm644 qt${pkgver}/LICENSE* -t "${pkgdir}"${_install_prefix}/share/licenses/${_realname}

  # Fix *.pri, *.bat and *.cmake files
  local PREFIX_WIN=$(cygpath -m ${MINGW_PREFIX})
  find "${pkgdir}${_install_prefix}/share/qt6" -type f \( -name '*.pri' -o -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${_install_prefix}/bin" -type f \( -name '*.bat' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
  find "${pkgdir}${_install_prefix}/lib/cmake" -type f \( -name '*.cmake' \) \
      -exec sed -i -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" {} \;
}
